package com.school.Service.impl;
import com.school.Service.StuService;
import com.school.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class StuServiceImpl implements StuService {
    @Override
    public void register() {
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
            SqlSession session = sqlSessionFactory.openSession();
            int count = session.insert("hello.insertStu", new Student("hi", "123"));
            session.commit();
            System.out.println(count);
            if (count > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
